package com.itheima.d5_Api_Arrays;

import java.util.Arrays;
import java.util.Comparator;

public class demo2 {
    public static void main(String[] args) {
        int[] ages = new int[]{34,12,24,23};
        Arrays.sort(ages);

        //自定义排序，降序
        Integer[] arr = {34,12,24,23};//需要包装类型的变量
        Arrays.sort(arr, new Comparator<Integer>() {
            @Override
            /**
             * L>R,return>0
             * L<R,return<0
             * L=R,return=0
             */
            public int compare(Integer o1, Integer o2) {
                //return o1-o2; //默认升序
                return o2-o1; //降序
            }
        });

        System.out.println(Arrays.toString(arr));
        //------------------------------------------

        //对象类型的比较器
        student[] s = new student[2];
        s[0] = new student("John",19,"m");
        s[1] = new student("Jack",25,"m");
        //System.out.println(Arrays.toString(s));//默认打印地址，需要重写toString
        //Arrays.sort(s);//会报错
        Arrays.sort(s, new Comparator<student>() {
            @Override
            public int compare(student o1, student o2) {
                return (o2.getAge()- o1.getAge());
                //浮点型比较
                //return Double.compare(o1.getAge()-o2.getAge());
            }
        });
        System.out.println(Arrays.toString(s));

    }
}
